package org.mcflier.appuntamento.main;

import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;

import org.mcflier.appuntamento.core.Factory;
import org.mcflier.appuntamento.core.exception.ModulesException;
import org.mcflier.appuntamento.core.exception.PropertiesException;
import org.mcflier.appuntamento.core.logger.LoggerManager;
import org.mcflier.appuntamento.core.properties.PropertiesManager;
import org.mcflier.appuntamento.data.model.ImdbList;
import org.mcflier.appuntamento.data.model.Torrent;
import org.mcflier.appuntamento.data.model.TorrentCategory;
import org.mcflier.appuntamento.db.DBManager;
import org.mcflier.appuntamento.db.exception.DatabaseConnectionException;
import org.mcflier.appuntamento.db.exception.DatabaseServerException;
import org.mcflier.appuntamento.io.driver.db.JDBCDriver;
import org.mcflier.appuntamento.io.driver.rss.imdb.RssImdbDriver;
import org.mcflier.appuntamento.io.exception.JDBCConnectionException;
import org.mcflier.appuntamento.io.exception.JDBCStatementException;
import org.mcflier.appuntamento.io.exception.RSSConnectionException;
import org.mcflier.appuntamento.io.exception.RSSParsingException;

public class Matching {

	private static final String FIRST_EXEC_TAG = "first_exec";

	public static void main(String[] args) {
		JDBCDriver jdbc;
		RssImdbDriver rssImdb;
		DBManager db;
		Logger logger;
		PropertiesManager propertiesManager;
		Properties properties;
		try {
			Factory factory = Factory.getInstance();
			jdbc = (JDBCDriver)factory.getModuleInstance(Factory.MODULE_JDBC);
			rssImdb = (RssImdbDriver)factory.getModuleInstance(Factory.MODULE_RSS_IMDB);
			db = (DBManager)factory.getModuleInstance(Factory.MODULE_DB);
			logger = ((LoggerManager)factory.getModuleInstance(Factory.MODULE_LOGGER)).getLogger(Matching.class.getName());
			propertiesManager = (PropertiesManager)factory.getModuleInstance(Factory.MODULE_PROPERTIES); 
			properties = propertiesManager.getProperties();

			try {
				db.startDatabase();
				if(Boolean.parseBoolean(properties.getProperty(FIRST_EXEC_TAG, "true"))){
					logger.info("Reseting the database");
					propertiesManager.addProperty(FIRST_EXEC_TAG, "false");
					propertiesManager.storeProperties();
					db.resetDatabase();
				}

				ImdbList list = new ImdbList("Appuntamento", "http://rss.imdb.com/list/gTAiBvBvKfE");
				System.out.println("Added list: "+jdbc.addList(list));
				rssImdb.fillList(list);

				List<ImdbList.IMDBListItem> items = list.getItems();
				for (ImdbList.IMDBListItem item : items) {
					String link = item.getLink();
					if(link.endsWith("/"))
						link = link.substring(0, link.length()-1);
					link = link.substring(link.lastIndexOf("/")+1);
					System.out.print(link+" ");

					String name = item.getTitle();
					if(name.endsWith(")")){
						if(name.contains("(")){
							try {
								if(Integer.parseInt(name.substring(name.lastIndexOf("(")+1, name.length()-1))>1900);
									name = name.substring(0, name.lastIndexOf("(")-1);
							} catch (NumberFormatException e) {
								
							}
						}
					}
					
					System.out.println(name);
					List<Torrent> torrents = jdbc.getTorrentsLike(name, TorrentCategory.HD_CATEGORY);
					for (Torrent torrent : torrents) {
						System.out.println(torrent.getTitle()+" / "+torrent.getResolution());
					}
					
					System.out.println("\n\n");
				}
			} catch (JDBCConnectionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JDBCStatementException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (RSSConnectionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (RSSParsingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (PropertiesException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (DatabaseServerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (DatabaseConnectionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			db.stopDatabase();
		} catch (ModulesException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
